const { mysql } = require('../../mysql.js');
//获取分类列表导航栏
async function categoryNav(ctx) {
  const categoryId = ctx.query.id
  //获取分类
  const currentNav = await mysql('nideshop_category').where({
    'id': categoryId
  }).select()
  //获取它的同类
  const navData = await mysql('nideshop_category').where({
    'parent_id': currentNav[0].parent_id
  }).select()
  ctx.body = {
    'navData': navData,
    'currentNav': currentNav[0]
  }
}
//分类页面
async function indexAction(ctx) {
  const { id: categoryId } = ctx.query
  const data = await mysql('nideshop_category').where({ 'parent_id': 0 }).select()
  //-就是点击了一个分类之后，点回首页，再次点回来的时候应该还是那个分类
  const currentCategory = []
  if (categoryId) {
    currentCategory = await mysql('nideshop_category').where({
      'parent_id': categoryId
    }).select()
  }
  ctx.body = {
    'categoryList': data
  }
}
//点击左侧菜单获取分类商品
async function currentAction(ctx) {
  const { id: categoryId } = ctx.query
  const data = {}
  const currentOne = await mysql('nideshop_category').where({
    'id': categoryId
  }).select()
  const subList = await mysql('nideshop_category').where({
    'parent_id': currentOne[0].id
  }).select()
  data.currentOne = currentOne[0]
  data.currentOne.subList = subList
  ctx.body = {
    'data': data
  }
}
module.exports = {
  categoryNav,
  indexAction,
  currentAction
}